function[percentrisk,percentinspect]=compvscomp(alpha,lambda,bonus)
%initialize all variables
prisk=0.5;%assume random first choice
priskcorr=0.5;
compcash=0;
usercash=0;%money values
m=0;%variable to check if choice is repeated
o=0;%variable to check if inspection is repeated
reprate=0;%repetition rate
repcount=0;%repetition count
count=1;%number of choices
pinspect=0.5;
pinspectcorr=0.5;
reprate_insp=0;
repcount_insp=0;
inspect=round(rand);%random first inspection
choice=round(rand);%random first choice,1 is risky 0 is safe
w=0.15;
c=0.065;
p=0.5;
i=0.075;
C=0;I=0;
z=1;
for t=1:1000
    if inspect==1%inspection, 1st trial random(no algorithm)
        %update probability and money amounts
        if inspect==1%set I variable used in equation 7
            I=1;
        else
            I=0;
        end

        pinspect=pinspect+alpha*(I-pinspect);%update pinspect
        if o==inspect
            repcount_insp=repcount_insp+1;
            reprate_insp=repcount_insp/count;%update the reprate
        end
        reprateex_insp=pinspect^2+(1-pinspect)^2;

        pinspectcorr=pinspect+lambda*(reprateex_insp-reprate_insp);%pinspectcorr accounting for reprate
        %update the subjective desirability of each possible choice
        SDrisky=(1-pinspectcorr)*w + (pinspectcorr)*0;%risky
        SDcertain=(1-pinspectcorr)*(w-c) + (pinspectcorr)*(w-c);%certain
        SDinspect=(1-priskcorr)*(p-w-i) + (priskcorr)*(-i);%inspect
        SDnotinspect=(1-priskcorr)*(p-w) + priskcorr*(-w);%notinspect
        SDworker=SDrisky/(SDrisky+SDcertain);
        SDemployer=SDinspect/(SDinspect+SDnotinspect);
        prob_choice=1/(1+exp(-10*SDworker+5));
        rand;
        if prob_choice==0.5
            choice=round(rand);
        elseif rand>=prob_choice
            choice=1;
        else
            choice=0;
        end

        o=inspect;%store the employer choice
        employer_choice(t)=inspect;%create a matrix of the employer choices
        SDplot(t)=SDworker;
        probability(t)=prob_choice;
        SDplot2(t)=SDemployer;

        if choice==1%update cash and tell user what happened based on choice
            display('i caught you.');
            usercash;
            compcash=compcash-i;
        elseif choice==0
            display('keep up the good work.');
            usercash=usercash +(w-c);
            compcash=compcash +(p-w-i);
        end

        %update probabilities and rep rate
        if choice==1%set C variable used in equation 7
            C=1;
        else
            C=0;
        end

        prisk=prisk+alpha*(C-prisk);%update prisk
        if m==choice
            repcount=repcount+1;%update reprate if choice was repeated
            reprate=repcount/count;
        end

        reprateex=prisk^2+(1-prisk)^2;

        priskcorr=prisk+lambda*(reprateex-reprate);%update priskcorr accounts for reprate

        prob_inspect=1./(1+exp(-10*SDemployer+5));
        rand;
        if prob_inspect==0.5
            inspect=round(rand);
        elseif rand>=prob_inspect
            inspect=1;
        else
            inspect=0;
        end

        m=choice;%store the worker choice
        workerchoice(t)=choice;%create a matrix of the workers choices
        probability2(t)=prob_inspect;

    else %no inspection
        if inspect==1%set I variable used in equation 7
            I=1;
        else
            I=0;
        end

        pinspect=pinspect+alpha*(I-pinspect);%update pinspect
        if o==inspect
            repcount_insp=repcount_insp+1;
            reprate_insp=repcount_insp/count;
        end
        reprateex_insp=pinspect^2+(1-inspect)^2;%update reprate and pinspectcorr

        pinspectcorr=pinspect+lambda*(reprateex_insp-reprate_insp);
        %update the subjective desirability of each possible choice
        SDrisky=(1-pinspectcorr)*w + (pinspectcorr)*0%risky
        SDcertain=(1-pinspectcorr)*(w-c) + (pinspectcorr)*(w-c)%certain
        SDinspect=(1-priskcorr)*(p-w-i) + (priskcorr)*(-i);%inspect
        SDnotinspect=(1-priskcorr)*(p-w) + priskcorr*(-w);%notinspect
        SDworker=SDrisky/(SDrisky+SDcertain);
        SDemployer=SDinspect/(SDinspect+SDnotinspect);
        prob_choice=1/(1+exp(-10*SDworker+5));
        rand;
        if prob_choice==0.5
            choice=round(rand);
        elseif rand>=prob_choice
            choice=1;
        else
            choice=0;
        end


        if choice==1%update money based on choice
            display('no inspection, you got lucky.');
            usercash=usercash+w;
            compcash=compcash-w;
        elseif choice==0
            display('no inspection.');
            usercash=usercash +(w-c);
            compcash=compcash +(p-w);
        end

        workerchoice(t)=choice;%add to matrices of employer and worker choices
        employer_choice(t)=inspect;
        SDplot(t)=SDworker;
        probability(t)=prob_choice;
        SDplot2(t)=SDemployer;

        if m==choice
            repcount=repcount+1;
            reprate=repcount/count;
        end

        if o==inspect
            priskcorr=priskcorr+bonus;%inspection bonus so that computer does not get stuck picking not inspect
        end
        prob_inspect=1/(1+exp(-10*SDemployer+5));
        rand;
        if prob_inspect==0.5
            inspect=round(rand);
        elseif rand>=prob_inspect
            inspect=1;
        else
            inspect=0;
        end

        o=inspect;
        probability2(t)=prob_inspect;
    end
    if t-19>0%plot of running avg. of choices
        runningwork(z)=sum(workerchoice(t-19:t)/20);
        runninginspect(z)=sum(employer_choice(t-19:t)/20);
        z=z+1;
    end
    count=count+1;
end
% runningwork
% runninginspect
plot(runningwork)
hold on;
plot(runninginspect,'color','red')
ylim([0 1]);
hold on;
x=1:1000;
y=i/w%equilibrium line for employee
plot(x,y)

hold on;
s=1:1000;
t=c/w%equilibrium line for employer
plot(s,t)
figure;
SDplot=sort(SDplot);%plot of SD vs. probability->logistic
SDplot2=sort(SDplot2);
probability=sort(probability);
probability2=sort(probability2);
plot(SDplot,probability,'marker','s')%worker
hold on
plot([0.5 0.5],[0 1])
hold on
plot([0 1], [0.5 0.5])
xlim([0 1])
ylim([0 1])
hold on;
xlim([0 1])
ylim([0 1])
plot(SDplot2,probability2,'color','red','marker','s')%employer
usercash
compcash
% figure
%  plot(workerchoice)
%  hold on
%  plot(employer_choice,'Color','red')
% figure;
riskcount=sum(workerchoice(500:1000));%count the choices of the last 500 trials and find a percentage of choice
inspectcount=sum(employer_choice(500:1000));
percentrisk=riskcount/501
percentinspect=inspectcount/501
plot(percentinspect,'marker','x','color','red')
hold on
plot(percentrisk,'marker','o')
end

